// pages/songDetail/songDetail.js
import { REQUEST_SONG_DETAIL, REQUEST_SONG_URL } from "../../api/index.js";
import pubsub from 'pubsub-js'
import moment from 'moment'
const appInstance = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPlay: false, // 音乐是否播放
    song: {}, // 歌曲详情对象
    musicId: "", // 音乐id
    musicLink: "", // 音乐的链接
    currentTime: "00:00", // 实时时间
    durationTime: "00:00", // 总时长
    currentWidth: 0, // 实时进度条的宽度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const musicId = options.musicId;
    this.backgroundAudioManager = wx.getBackgroundAudioManager();
    this.getMusicInfo(musicId);
    this.setData({ musicId });
    this.bindAudioEventState(musicId);
    
  },
  bindAudioEventState(musicId) {
    const { changePlayState } = this;
    const { isMusicPlay, musicId: GlobalMusicId } = appInstance.globalData;
    if (isMusicPlay && GlobalMusicId === musicId) {
      this.setData({ isPlay: true });
    }
    this.backgroundAudioManager.onPlay(changePlayState.bind(null, true, musicId));
    this.backgroundAudioManager.onPause(changePlayState.bind(null, false));
    this.backgroundAudioManager.onStop(changePlayState.bind(null, false));
    this.backgroundAudioManager.onEnded(() => {
      pubsub.publish('switchType', 'next');
      this.setData({ currentTime: "00:00", currentWidth: 0 });
    });
    this.backgroundAudioManager.onTimeUpdate(() => {
      const currentTime = moment(this.backgroundAudioManager.currentTime * 1000).format('mm:ss');
      const currentWidth = this.backgroundAudioManager.currentTime / this.backgroundAudioManager.duration * 450;
      this.setData({ currentTime, currentWidth });
    });
  },
  changePlayState(isPlay, musicId) {
    this.setData({ isPlay });
    appInstance.globalData.isMusicPlay = isPlay;
    musicId && (appInstance.globalData.musicId = musicId);
  },
  handleSwitch(event) {
    const type = event.currentTarget.id;
    pubsub.publish('switchType', type);
    pubsub.subscribe('musicId', (_, musicId) => {
      this.getMusicInfo(musicId);
      this.musicControl(true);
      pubsub.unsubscribe('musicId');
    });
  },
  handleMusicPlay() {
    const isPlay = !this.data.isPlay;
    this.musicControl(isPlay, this.data.musicLink);
  },
  async getMusicInfo(musicId) {
    const { songs } = await REQUEST_SONG_DETAIL({ids: musicId});
    const durationTime = moment(songs[0].dt).format('mm:ss');
    this.setData({
      song: songs[0],
      musicId,
      durationTime
    })
    
    wx.setNavigationBarTitle({
      title: songs[0].name
    })
  },
  async musicControl(isPlay, musicLink) {
    const { musicId, song } = this.data;
    if (isPlay) {
      if (!musicLink) {
        const { data } = await REQUEST_SONG_URL({id: musicId });
        musicLink = data[0].url;
        this.setData({ musicLink });
      }

      this.backgroundAudioManager.title = song.name;
      this.backgroundAudioManager.src = musicLink;
    } else {
      this.backgroundAudioManager.pause();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
